package tw.com.softleader.fire.setting.web;

import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import tw.com.softleader.fire.setting.entity.AcuAddrEntity;
import tw.com.softleader.fire.setting.entity.AcuEntity;
import tw.com.softleader.fire.setting.service.AcuService;

import com.opensymphony.xwork2.Action;

public class AcuAction {
	private Logger log = LoggerFactory.getLogger(AcuAction.class);

	@Autowired
	private AcuService acuService;
	private List<AcuEntity> models;
	private AcuEntity model;
	private String searchBox;
	
	public AcuEntity getModel() {
		return model;
	}
	public void setModel(AcuEntity model) {
		this.model = model;
	}
	public List<AcuEntity> getModels() {
		return models;
	}
	public void setModels(List<AcuEntity> models) {
		this.models = models;
	}
	
	@SkipValidation
	public String execute() {
		log.debug("execute~~");
		try {
			models = acuService.getAll();
			
			log.debug("Models = {}",models);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "success";
	}
	
	public String delete() {
		log.debug("delete...");
		log.debug("{}",model);
		try {
			acuService.delete(model);
		} catch (Exception e) {
			log.debug("!!deleteFail!!");
			e.printStackTrace();
		}
		models = acuService.getAll();
		log.debug("Models = {}", models);
		return Action.SUCCESS;
	}
	
	public String update() {
		log.debug("update...");
		log.debug("{}",model);
		model.setModifiedTime(LocalDateTime.now());
		try {
			acuService.update(model);
		} catch (Exception e) {
			log.debug("!!updateFail!!");
			e.printStackTrace();
		}
		models = acuService.getAll();
		
		return Action.SUCCESS;
	}
	
	public String getSearchBox() {
		return searchBox;
	}
	public void setSearchBox(String searchBox) {
		this.searchBox = searchBox;
	}
	
	public String search() {
		log.debug("search~~~");
		log.debug("Model = {}", model);
		log.debug("searchBox = {}", this.searchBox);
		model = new AcuEntity();
		model.setAcuNo(this.searchBox);
		models = acuService.search(model);
		
		log.debug("Models = {}", models);
		
		return Action.SUCCESS;
	}
	
	
}
